<?php

namespace App\Admin\Controllers;

use App\Models\Help;

use App\Models\HelpClassify;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class HelpController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('帮助中心');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Help::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->name('标题')->editable();

            //状态列
            $states = [
                'on' => ['value' => 1, 'text' => '发布', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
            ];
            $grid->status('发布')->switch($states);

            //属于的分类
            $classify = HelpClassify::all();
            $options = [];
            foreach ($classify as $item) {
                $options[$item['id']] = $item['name'];
            }
            $grid->help_classify_id('分类')->editable('select', $options);


            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');

            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();
                //分类查询
                $filter->equal('help_classify_id', '分类')->select(HelpClassify::all()->pluck('name', 'id'));
                // 设置created_at字段的范围查询
                $filter->between('created_at', '发布时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Help::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '标题')->rules('required');

            $states = [
                'on' => ['value' => 1, 'text' => '发布', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
            ];
            $form->switch('status', '发布')->states($states);

            //属于的分类
            $form->select('help_classify_id','分类')->options(function(){
                $classify = HelpClassify::all();
                $options = [];
                foreach ($classify as $item) {
                    $options[$item['id']] = $item['name'];
                }
                return $options;
            });

            //富文本编译
            $form->kindeditor('content','内容');

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
        });
    }
}
